/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    vector<int> nextLargerNodes(ListNode* head) {
        vector<pair<int, int>> stk;
        vector<int> ans;
        int i = 0;
        ListNode* temp = head;
        while (temp != NULL)
        {
            ans.push_back(0);
            while (!stk.empty() && temp->val > stk.back().second)
            {
                ans[stk.back().first] = temp->val;
                stk.pop_back();
            }
            stk.push_back(make_pair(i, temp->val));
            temp = temp->next;
            i++;
        }
        return ans;
    }
};